package com.aestheticshare.main.introduction;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.aestheticshare.main.R;
import com.aestheticshare.main.Utils;
import com.google.analytics.tracking.android.EasyTracker;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.ViewById;
import com.viewpagerindicator.CirclePageIndicator;

@EActivity(R.layout.activity_introduction)
public class IntroductionActivity extends SherlockFragmentActivity{
	SharedPreferences settings;
	@ViewById(R.id.introductionPager) ViewPager pager;
	CirclePageIndicator indicator;
	IntroductionPagerAdapter adapter;
	
	@AfterViews
	public void initialize(){
		
		EasyTracker.getInstance().setContext(this);  
		
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		
		adapter = new IntroductionPagerAdapter(getSupportFragmentManager());
		pager.setAdapter(adapter);

        
        indicator = (CirclePageIndicator)findViewById(R.id.introductionIndicator);
        indicator.setViewPager(pager);
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            	settings.edit().putInt(Utils.KEY_INTRO_PAGE_POSITION, position).commit();
        		ImageButton btn = ((IntroductionFragment)adapter.getItem(position)).introductionButton;
            	if(adapter.getCount()-1==position && btn.getVisibility()==View.GONE){
            		btn.setImageResource(R.drawable.continue_btn);
            		btn.setOnTouchListener(Utils.IMAGE_BUTTON_ONCLICK_EFFECT_TOUCH_LISTENER);
            		btn.setVisibility(View.VISIBLE);
            		btn.startAnimation(AnimationUtils.loadAnimation(IntroductionActivity.this, android.R.anim.fade_in));
            		btn.setOnClickListener(new View.OnClickListener(){
						@Override
						public void onClick(View arg0) {
							settings.edit().putInt(Utils.KEY_INTRO_PAGE_POSITION, Integer.MAX_VALUE).commit();
							finish();
						}
            		});
            	}
            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        
        int position = getIntent().hasExtra(Utils.KEY_INTRO_PAGE_POSITION) ? getIntent().getIntExtra(Utils.KEY_INTRO_PAGE_POSITION, 0) : settings.getInt(Utils.KEY_INTRO_PAGE_POSITION, 0);
        if(position == Integer.MAX_VALUE){
        	finish();
		}else{
			indicator.setCurrentItem(position);
		}

	}
	
	@Override
	public void finish(){
		super.finish();
		overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
	}

	@Override
	public void onBackPressed() {
		settings.edit().putInt(Utils.KEY_INTRO_PAGE_POSITION, Integer.MAX_VALUE).commit();
		super.onBackPressed();
	}

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}
}
